package com.leased.service.impl;

import com.leased.exception.DatabaseException;
import com.leased.interceptor.PageMybatisInterceptor;
import com.leased.mapper.ZUsersMapper;
import com.leased.mapper.manual.ICustomizedCUserMapper;
import com.leased.mapper.manual.ICustomizedZUserMapper;
import com.leased.pojo.ZUsers;
import com.leased.pojo.ZUsersExample;
import com.leased.request.zuser.ZUserListRequest;
import com.leased.response.UserCountResponse;
import com.leased.response.ZUserResponse;
import com.leased.service.ZUserService;
import com.leased.utils.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by shencx on 2017/7/28.
 */
@Service
public class ZUserServiceImpl implements ZUserService{

    Logger LOG = LoggerFactory.getLogger(ZUserServiceImpl.class);

    @Resource
    private ZUsersMapper zUsersMapper;

    @Resource
    private ICustomizedZUserMapper customizedZUserMapper;

    @Resource
    private ICustomizedCUserMapper customizedCUserMapper;

    /**
     * 添加专线用户信息
     * @param zUsers
     * @return
     * @throws DatabaseException
     */
    @Override
    public Integer addZUserInfo(ZUsers zUsers) throws DatabaseException {
        try {
            if (zUsers == null){
                LOG.error("addZUserInfo 信息为空",zUsers);
                return Integer.valueOf(0);
            }
            zUsers.setRegisterTime(new Date());
            int val = zUsersMapper.insertSelective(zUsers);
            if (val > 0){
                return zUsers.getId();
            }else {
                return Integer.valueOf(0);
            }
        }catch (Throwable e) {
            LOG.error("addZUserInfo 发生异常",e,zUsers);
            throw new DatabaseException(e.getMessage());
        }
    }

    @Override
    public boolean updateZUserInfo(ZUsers zUsers) throws DatabaseException {
        try {
            if (zUsers == null){
                LOG.error("updateZUserInfo 信息为空",zUsers);
                return false;
            }
            Integer val = zUsersMapper.updateByPrimaryKeySelective(zUsers);
            return val>0?true:false;
        }catch (Throwable e) {
            LOG.error("updateZUserInfo 发生异常",e,zUsers);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取专线用户列表
     * @param zUserListRequest
     * @return
     * @throws DatabaseException
     */
    @Override
    public Page<ZUserResponse> fetchZUserInfoPage(ZUserListRequest zUserListRequest) throws DatabaseException {
        try {
            if (zUserListRequest == null){
                LOG.error("fetchZUserInfoPage 信息为空",zUserListRequest);
                return null;
            }
            PageMybatisInterceptor.startPage(zUserListRequest.getPageNum(),zUserListRequest.getPageSize());
            customizedZUserMapper.findZUserInfoList(zUserListRequest);
            Page<ZUserResponse> page = PageMybatisInterceptor.endPage();
            return page;
        }catch (Throwable e) {
            LOG.error("fetchZUserInfoPage 发生异常",e,zUserListRequest);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取专线详情
     * @param zUserListRequest
     * @return
     * @throws DatabaseException
     */
    @Override
    public ZUsers fetchZUserDetail(ZUserListRequest zUserListRequest) throws DatabaseException {
        try {
            if (zUserListRequest == null){
                LOG.error("fetchZUserDetail 信息为空",zUserListRequest);
                return null;
            }
            ZUsersExample example = new ZUsersExample();
            ZUsersExample.Criteria criteria = example.createCriteria();
            if (zUserListRequest.getId() != null) {
                criteria.andIdEqualTo(zUserListRequest.getId());
            }
            if (StringUtils.isNoneBlank(zUserListRequest.getUserName())){
                criteria.andUserNameEqualTo(zUserListRequest.getUserName());
            }
            if (StringUtils.isNoneBlank(zUserListRequest.getPhoneNumber())){
                criteria.andPhoneNumberEqualTo(zUserListRequest.getPhoneNumber());
            }
            List<ZUsers> list = zUsersMapper.selectByExample(example);
            if (list == null || list.size() == 0){
                return null;
            }else {
                return list.get(0);
            }
        } catch (Throwable e) {
            LOG.error("fetchZUserDetail 发生异常",e,zUserListRequest);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取专线注册用户数
     * @return
     * @throws DatabaseException
     */
    public UserCountResponse fetchCountNum() throws DatabaseException {
        UserCountResponse userCountResponse = new UserCountResponse();
        try {
            Integer zuserNum = customizedZUserMapper.getAllZUserCount();
            userCountResponse.setZuserCount(zuserNum);
            Integer zCurrentNum = customizedZUserMapper.getCurrentDayCount();
            userCountResponse.setZuserCurrentCount(zCurrentNum);
            Integer cuserNum = customizedCUserMapper.getAllCUserCount();
            userCountResponse.setCuserCount(cuserNum);
            Integer cCurrentNum = customizedCUserMapper.getCUserCurrentDayCount();
            userCountResponse.setCuserCurrentCount(cCurrentNum);
            return userCountResponse;
        }  catch (Throwable e) {
            LOG.error("fetchCountNum 发生异常",e);
            throw new DatabaseException(e.getMessage());
        }
    }

}
